package com.zut.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zut.commonutils.R;
import com.zut.eduservice.entity.Comactiontable;
import com.zut.eduservice.service.ComactiontableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-01-02
 */
@RestController
@RequestMapping("/eduservice/comactiontable")
@CrossOrigin
public class ComactiontableController {
    @Autowired
    private ComactiontableService comactiontableService;
    //如果修改视频信息不存在则直接添加
    @PostMapping("addComState/{comType}")
    public R addOrUpdataComState( @PathVariable int comType,@RequestBody  Comactiontable comactiontable) {
            comactiontable.setComState(0);
            //视频输入切换
            if (comType==0){
             comactiontable.setComType(0);
            }
            //视频比列调整
            if (comType==1){
                comactiontable.setComType(1);
            }
        if (comType==2){
            comactiontable.setComType(2);
        }
            boolean save = comactiontableService.save(comactiontable);
            if (save) {
                return R.ok();
            } else {
                return R.error();
            }


    }

   //如果修改视频信息存在则修改信息内容
    @PostMapping("updataComState/{comType}")
    public R updataComState(@PathVariable int comType,@RequestBody Comactiontable comactiontable) {
        comactiontable.setComState(0);
        //视频输入切换
        if (comType==0){
            comactiontable.setComType(0);
        }
        //视频比列调整
        if (comType==1){
            comactiontable.setComType(1);
        }
        if (comType==2){
            comactiontable.setComType(2);
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("student_id",comactiontable.getStudentId());
        updateWrapper.set("addition",comactiontable.getAddition());
        boolean flag  = comactiontableService.update(comactiontable,updateWrapper);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("getComInformmation/{studentId}")
    public R findComById(@PathVariable String studentId) {
       QueryWrapper<Comactiontable> queryWrapper =new QueryWrapper<>();
       queryWrapper.eq("student_id",studentId);
        List<Comactiontable> comactiontable=  comactiontableService.list(queryWrapper);
        System.out.println(comactiontable);
        if(comactiontable.isEmpty()){
           return R.ok().data("records",0);
        }else {
            return R.ok().data("records",1);
        }

    }
}

